package com.pzl.simple;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.pzl.util.DialogHelper;
import com.pzl.view.ImagePreviewView;
import com.pzl.view.PreviewerViewPager;
import com.pzl.view.TimeButton;
import com.pzl.view.zoomimageview.ZoomImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ViewLibActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.t_btn)
    TimeButton tb;

    @BindView(R.id.ziv)
    ZoomImageView ziv;

    @BindView(R.id.pvp)
    PreviewerViewPager previewerViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_lib);
        ButterKnife.bind(this);
        timeBtn();
        zoomImageView();
        previewerViewPager();
    }

    private void timeBtn() {
        tb.onCreate(null);
        tb.setBeforBg(40);
        tb.setAfterBg(R.color.colorAccent);
        tb.setBeforBg(R.color.colorPrimaryDark);
        tb.setLenght(50);
        tb.setRun(true);
        tb.setTextAfter("之后");
        tb.setTextBefore("之前");
    }

    private void zoomImageView() {
        ziv.enable();
    }

    private void previewerViewPager() {
        previewerViewPager.setAdapter(new PicPagerAdapter(this));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tb.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialogHelper:
                Toast.makeText(this,"btn_dialogHelper",Toast.LENGTH_LONG).show();
                DialogHelper.getMessageDialog(this, "message", android.R.style.Theme_DeviceDefault_Light_Dialog).create().show();
                break;
        }
    }

    class PicPagerAdapter extends PagerAdapter {

        Context ctx;

        LayoutInflater inflater;

        List<View> views = new ArrayList<>();

        public PicPagerAdapter(Context ctx) {
            this.ctx = ctx;
            inflater = LayoutInflater.from(ctx);
            int i = 0;
            while (i <= 4) {
                View view = inflater.inflate(R.layout.layout_preview_pic, null);
                views.add(view);
                i++;
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v = views.get(position);
            ImagePreviewView ipv = (ImagePreviewView) v.findViewById(R.id.ipv);
            ipv.setImageResource(R.drawable.p1);
            container.removeAllViews();
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
